package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.DormitoryAdmin;
import com.example.studentsRoom.form.RuleForm;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.DormitoryAdminMapper;
import com.example.studentsRoom.service.IDormitoryAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
@Slf4j
public class DormitoryAdminService extends ServiceImpl<DormitoryAdminMapper, DormitoryAdmin> implements IDormitoryAdminService {

    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    /**
     * 登录
     * @param ruleForm
     * @return
     */
    @Override
    public ResultVo login(RuleForm ruleForm) {

        //1.判断用户名是否存在
        QueryWrapper<DormitoryAdmin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("d_account",ruleForm.getAccount()).eq("d_password",ruleForm.getPassword());
        DormitoryAdmin dormitoryAdmin=null;
        ResultVo resultVo=new ResultVo();
        try{
            dormitoryAdmin= this.dormitoryAdminMapper.selectOne(queryWrapper);
        }catch (Exception e){
            resultVo.setCode(-1);
        }


        if (dormitoryAdmin==null){
            resultVo.setCode(-1);
        }else{
            //2.判断密码
            if(!dormitoryAdmin.getDPassword().equals(ruleForm.getPassword())){
                resultVo.setCode(-2);
            }else{
                resultVo.setCode(0);
                resultVo.setData(dormitoryAdmin);
            }
        }
        return resultVo;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<DormitoryAdmin> dormitoryAdminPage=new Page<>(page,size);

        Page<DormitoryAdmin> resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);

        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setData(resultPage.getRecords());
        return pageVo;
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        //模糊查询加分页
        Page<DormitoryAdmin> dormitoryAdminPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<DormitoryAdmin> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")){
            resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);
        }else{
            QueryWrapper<DormitoryAdmin> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, queryWrapper);
        }
        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setData(resultPage.getRecords());
        return pageVo;
    }
}
